package com.shijichaoxing.controller;

import com.shijichaoxing.entity.Admin;
import com.shijichaoxing.service.AdminService;
import com.shijichaoxing.utils.PageParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.Date;

/**
 * @author zj
 */
@Controller
public class AdminController {

    @Resource
    private AdminService adminService;

    @RequestMapping(value = "/admin/login.do", method = RequestMethod.POST)
    @ResponseBody
    public boolean login(@RequestParam String realName, @RequestParam String password) {
        Admin admin = new Admin();
        admin.setRealName(realName);
        admin.setPassword(password);
        return adminService.login(admin);
    }

    @RequestMapping(value = "/admin/addAdmin.do", method = RequestMethod.POST)
    @ResponseBody
    public boolean addAdmin(@RequestParam String realName, @RequestParam String password) {
        Admin admin = new Admin();
        admin.setRealName(realName);
        admin.setPassword(password);
        boolean result = adminService.addAdmin(admin);
        return result;
    }

    @RequestMapping(value = "/admin/updateAdmin.do", method = RequestMethod.POST)
    @ResponseBody
    public boolean updateAdmin(@RequestParam Integer id, @RequestParam String realName, @RequestParam String password) {
        Admin admin = new Admin();
        admin.setId(id);
        admin.setRealName(realName);
        admin.setPassword(password);
        boolean result = adminService.updateAdmin(admin);
        return result;
    }

    @RequestMapping(value = "/admin/findAdminById.do", method = RequestMethod.GET)
    @ResponseBody
    public Admin findAdminById(@RequestParam Integer id) {
        Admin admin = adminService.findAdminById(id);
        return admin;
    }

    @RequestMapping(value = "/admin/findAdminByPage.do", method = RequestMethod.GET)
    @ResponseBody
    public PageParam<Admin> findAdminByPage(
            @RequestParam int currPage,
            @RequestParam int pageSize,
            @RequestParam String indexName,
            @RequestParam boolean isAsc) {
        PageParam<Admin> pageParam = new PageParam<>(currPage, pageSize);
        adminService.findAdminByPage(pageParam, indexName, isAsc);
        return pageParam;
    }

    @RequestMapping(value = "/admin/deleteById.do", method = RequestMethod.DELETE)
    @ResponseBody
    public boolean deleteById(@RequestParam Integer id) {
        boolean result = adminService.deleteById(id);
        return result;
    }
}
